﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Resto.CostControl.Core.Models.SysSettings;
using System.Data.SqlClient;

namespace Resto.CostControl.Core.Services
{
    public class RoleService
    {
        private DbContext context = null;
        public RoleService(DbContext ctx)
        {
            context = ctx;
        }

        public IList<RoleInfo> QueryUserPrivilegeRoles(string userCode, string privilegeCode)
        {
            string sql = @"select Distinct A.* from RoleInfos A
                    INNER JOIN RolePrivilegeRelations B On A.RoleCode=B.RoleCode
                    INNER JOIN RoleUserRelations C On A.RoleCode=C.RoleCode
                    WHERE C.UserCode=@UserCode and B.PrivilegeCode=@PrivilegeCode 
                    Order By A.SeqNo";

            List<SqlParameter> plist = new List<SqlParameter>();
            plist.Add(new SqlParameter("@UserCode", userCode));
            plist.Add(new SqlParameter("@PrivilegeCode", privilegeCode));

            var list = context.Database.SqlQuery<RoleInfo>(sql, plist.ToArray()).ToList();

            return list;
        }
    }
}
